//
//  IMISoundAppDelegate.m
//  IMISound
//
//  Created by Travis on 09-8-11.
//  Copyright Exmart 2009. All rights reserved.
//

#import "AppDelegate.h"
#import <IMIBase/IMIBase.h>
#import "IMISoundPlayer.h"

#import "IMISoundListener.h"
#import "IMISoundRecorder.h"
@implementation AppDelegate

@synthesize window;

IMISoundListener *sl;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
	window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	window.backgroundColor = [UIColor whiteColor];
	window.multipleTouchEnabled=YES;
    [window makeKeyAndVisible];
	
	[IMISoundPlayer sharedPlayer].muted=YES;
	
	int w=100,h=28;
	
	UIButton *b1,*b2,*b3,*b4,*b5;
	b1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
	b1.tag=0;
	b1.frame=CGRectMake(0, 30, w, h);
	[b1 setTitle:@"bell" forState:UIControlStateNormal];
	[b1 addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
	[window addSubview:b1];
	
	b2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
	b2.tag=1;
	b2.frame=CGRectMake(0, 60, w, h);
	[b2 setTitle:@"broom" forState:UIControlStateNormal];
	[b2 addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
	[window addSubview:b2];
	
	b3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
	b3.tag=2;
	b3.frame=CGRectMake(0, 90, w, h);
	[b3 setTitle:@"cut" forState:UIControlStateNormal];
	[b3 addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
	[window addSubview:b3];
	
	b4=[UIButton buttonWithType:UIButtonTypeRoundedRect];
	b4.tag=3;
	b4.frame=CGRectMake(0, 120, w, h);
	[b4 setTitle:@"rake" forState:UIControlStateNormal];
	[b4 addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
	[window addSubview:b4];
	
	b5=[UIButton buttonWithType:UIButtonTypeRoundedRect];
	b5.tag=4;
	b5.frame=CGRectMake(0, 150, w, h);
	[b5 setTitle:@"water" forState:UIControlStateNormal];
	[b5 addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
	[window addSubview:b5];

	[[IMISoundListener sharedListener] listen];
	//取得平均音量
	[[IMISoundListener sharedListener] averagePower];
	
	//取得最大音量
	[[IMISoundListener sharedListener] peakPower];
	
	[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(soundL:) userInfo:nil repeats:YES];
	
}
-(void)soundL:(NSTimer*)t{
	if ([[IMISoundListener sharedListener] isListening]){
		IMILog(@"%f",[[IMISoundListener sharedListener] peakPower]);
	}else {
		IMILog(@"not listening");
	}

	//AudioQueueLevelMeterState *levels = [[IMISoundListener sharedListener] levels][0].mPeakPower;
}
-(void)play:(UIButton *)sender{
	int p=sender.tag;
	IMISoundRecorder *sr=[[IMISoundRecorder alloc] init];
	[sr startRecording];
	if(p==0){
		[[IMISoundPlayer sharedPlayer] playEffect:ResourceFilePath(@"bell.caf", nil)];
	}else if(p==1){
		[[IMISoundPlayer sharedPlayer] playBackgroundMusic:ResourceFilePath(@"toolbroom.caf", nil) loop:YES];
	}else if(p==2){
		[[IMISoundPlayer sharedPlayer] preloadEffect:ResourceFilePath(@"toolcut.caf", nil)];
		[[IMISoundPlayer sharedPlayer] playEffect:ResourceFilePath(@"toolcut.caf", nil)];
	}else if(p==3){
		[[IMISoundPlayer sharedPlayer] playEffect:ResourceFilePath(@"toolrake.caf", nil)];
	}else if(p==4){
		[[IMISoundPlayer sharedPlayer] playEffect:ResourceFilePath(@"waterMachine.caf", nil)];
	}else if(p==5){
		
	}
	[sr stopRecording];
	
}


- (void)dealloc {
    [window release];
    [super dealloc];
}


@end
